package sapling.web.admin;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sapling.dao.LandMassDao;
import sapling.form.TreeForm;
import sapling.model.LandMass;
import sapling.model.TreeWell;
import sapling.service.LandMassService;
import sapling.service.PushService;
import sapling.service.TreeWellService;
import sapling.utils.FileUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by ever on 2016/10/9.
 */
@Controller
@RequestMapping(value = "/admin/treeWell")
public class TreeWellAction {
    @Autowired
    private LandMassDao landMassDao;
    @Autowired
    private TreeWellService treeWellService;
    @Autowired
    private LandMassService landMassService;
    @Autowired
    private PushService  pushService;

    @RequestMapping("/treeWell")
    @ResponseBody
    public List<TreeWell> treeWell(Long id){
        List<TreeWell> treeWells=new ArrayList<>();
        LandMass landMass=landMassDao.findOne(id);
        if(Objects.nonNull(landMass)){
            treeWells=landMass.getTreeWells();
        }
        return treeWells;
    }

    @RequestMapping("/put")
    public String put(TreeForm treeForm){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String serid = sdf.format(new Date());
        int num=0;
        Boolean clump=true;
        if(2==treeForm.getClump()){
            clump=false;
        }
        Subject currentUser = SecurityUtils.getSubject();
        String username = currentUser.getSession().getAttribute("username").toString();
        String imgurl= FileUtil.savefile(treeForm.getFile());
        if(Objects.nonNull(treeForm.getTreeWell_id())&&treeForm.getTreeWell_id().size()>0){
            num=treeForm.getTreeWell_id().size();
            for (Long value : treeForm.getTreeWell_id()) {
                treeWellService.saveTreeWell(value, treeForm.getTreeSpecies(),treeForm.getChestBreadth(),-1,
                        treeForm.getCrownWidth(),-1
                        , treeForm.getHeight(),-1,
                        (Integer.parseInt(treeForm.getSalenumber())>0)
                        , num, username,
                        (treeForm.getSalenumber()==null||treeForm.getSalenumber().length()<1)?0:(Integer.parseInt(treeForm.getSalenumber())) , -1,
                        serid,
                        treeForm.getRemark()
                        ,imgurl,treeForm.getCost());

            }
            pushService.inform("finance","有"+num+"棵苗木需要录入产地价格");
        }
        return "redirect:/admin/landMass/treeWell?landMass_id="+treeForm.getLandMass_id();
    }

    @RequestMapping("/create")
    public String create(@RequestParam int way, @RequestParam double gap, @RequestParam Long landMass_id) {
        landMassService.plantWay(way, gap, landMass_id);
        return "redirect:/admin/landMass/treeWell?landMass_id="+landMass_id;
    }
}
